package com.qst.rongxiaoserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qst.rongxiaoserver.mapper.UserMapper;
import com.qst.rongxiaoserver.model.UserModel;
import com.qst.rongxiaoserver.model.dto.UserDto;
import com.qst.rongxiaoserver.service.IUserService;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserModel> implements IUserService {
    // 实现接口中的方法
    @Override
    public boolean updateUser(UserModel userModel) {
        // 根据用户名， 查询用户
        UserModel user = this.getById(userModel.getUserName());
        if(user==null){
            return false;
        }
        userModel.setPassword(user.getPassword());  // 使用原密码
        this.updateById(userModel);
        return true;
    }

    @Override
    public boolean updatePassword(UserDto userDto) {
        // 根据用户名， 查询用户
        UserModel user = this.getById(userDto.getUserName());
        if(user==null){
            return false;
        }
        user.setPassword(userDto.getPassword());  // 使用新密码
        this.updateById(user);
        return true;
    }

    @Override
    public Page<UserModel> getUserList(int pageNum, int pageSize, String phone, String nickName) {
        // 分页查询
        Page<UserModel> page1 = new Page<>(pageNum, pageSize);

        // 查询条件
        LambdaQueryWrapper<UserModel> queryWrapper = new LambdaQueryWrapper<>();
        if(phone!=null && !phone.isEmpty()){
            queryWrapper.eq(UserModel::getPhone,phone);   // 精确查询
        }
        if(nickName!=null && !nickName.isEmpty()){
            queryWrapper.like(UserModel::getNickName,nickName);   // 模糊查询
        }

        return this.page(page1, queryWrapper);
    }

}
